package com.self.war;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.myself.repositories.PostRepository;
import com.self.war.errorhandler.IAbtractErrorHandler;
import com.self.war.errorhandler.impl.DevErrorHandler;
import com.self.war.view.AdminView;
import com.self.war.view.LoginView;
import com.self.war.view.UserView;
import com.vaadin.annotations.Theme;
import com.vaadin.navigator.Navigator;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.UI;

@Theme("mytheme")
@SuppressWarnings("serial")
@Component
@Scope("prototype")
public class MyVaadinUI extends UI   {
	// services
	@Autowired(required = true)
	private PostRepository postRepository;
	// views
	@Autowired
	private UserView userView;
	@Autowired
	private AdminView adminView;
	@Autowired
	private LoginView loginView;
	
	@Autowired 
	IAbtractErrorHandler errorHandler;
	
	public static class Servlet extends VaadinServlet {
	}


	@Override
	protected void init(VaadinRequest request) {
		Navigator navigator = new Navigator(this, this);
		navigator.addView(LoginView.VIEW_NAME, loginView);
		navigator.addView(UserView.VIEW_NAME, userView);
		navigator.addView(AdminView.VIEW_NAME, adminView);
		navigator.navigateTo(LoginView.VIEW_NAME);
	}

	@PostConstruct
	private void initializeApplication(){
		VaadinSession.getCurrent().setErrorHandler(errorHandler);
		errorHandler.setParent(this);
		
	}
	
	
	public PostRepository getPostRepository() {
		return postRepository;
	}

	public void setPostRepository(PostRepository postRepository) {
		this.postRepository = postRepository;
	}
	public UserView getUserView() {
		return userView;
	}

	public void setUserView(UserView userView) {
		this.userView = userView;
	}

	public AdminView getAdminView() {
		return adminView;
	}

	public void setAdminView(AdminView adminView) {
		this.adminView = adminView;
	}

	public LoginView getLoginView() {
		return loginView;
	}

	public void setLoginView(LoginView loginView) {
		this.loginView = loginView;
	}
	 
}
